package vcp.client.utility;

import java.awt.Component;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
/**
 * <p> utility helper class </p>
 * @author VCP team
 *
 */
public class UtilityHelper
{
	private static UtilityHelper utilityHelper = new UtilityHelper();
	/**
	 * getter
	 * @return instance of utility helper
	 */
	public static UtilityHelper getUtilityHelperInstance()
	{
		return UtilityHelper.utilityHelper;
	}

	private PdfGenerator pdfGenerator;
	/**
	 * getter
	 * @return pdf generator
	 */
	public PdfGenerator getPdfGenerator()
	{
		// lazy initialization
		if (this.pdfGenerator == null)
		{
			this.pdfGenerator = new PdfGenerator();
		}
		return this.pdfGenerator;
	}
	/**
	 * constructor
	 */
	private UtilityHelper()
	{

	}
	/**
	 * checks if string is empty or null
	 * @param stringToVerify
	 * @return true if not empty or null
	 */
	public synchronized boolean isStringNullOrEmpty(String stringToVerify)
	{
		boolean isNullOrEmpty = false;
		if (stringToVerify == null || stringToVerify.isEmpty())
		{
			isNullOrEmpty = !isNullOrEmpty;
		}
		return isNullOrEmpty;
	}
	/**
	 * setter
	 * @param component
	 * @param status
	 */
	public void setComponentsEnabled(JComponent component, boolean status)
	{
		List<JComponent> components = this.getComponentsDescendingAndSelfTreeHierarchy(component);
		for (JComponent jComponent : components)
		{
			jComponent.setEnabled(status);
		}
	}
	/**
	 * getter
	 * @param component
	 * @return list of components
	 */
	public List<JComponent> getComponentsDescendingAndSelfTreeHierarchy(JComponent component)
	{
		List<JComponent> components = new ArrayList<>();
		if (component != null)
		{
			for (Component innerComponent : component.getComponents())
			{
				if (innerComponent instanceof JComponent)
				{
					components.addAll(this.getComponentsDescendingAndSelfTreeHierarchy((JComponent) innerComponent));
				}
			}

			components.add(component);
		}
		return components;
	}
	/**
	 * loads scales image
	 * @param imageName
	 * @param width
	 * @param height
	 * @return created image
	 */
	public ImageIcon loadScaledImage(String imageName, int width, int height)
	{
		ImageIcon createdImage = null;

		String path = String.format("src/resources/%s", imageName);
		try
		{
			File file = new File(path);
			Image loadedImage = ImageIO.read(file);
			createdImage = new ImageIcon(loadedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH));
		} catch (IOException e)
		{
			createdImage = null;
		}

		return createdImage;
	}
	/**
	 * checks if parameters are not null
	 * @param params
	 * @return true if not null
	 */
	public boolean isParamsNotNull(Object... params)
	{
		boolean isnonull = true;
		for (Object object : params)
		{
			if (object == null)
			{
				isnonull = false;
			}
			else if (object instanceof String)
			{
				isnonull = !this.isStringNullOrEmpty((String) object);
			}

			if (!isnonull)
			{
				break;
			}

		}
		return isnonull;
	}

}
